package com.wxy.xr.dao.impl;

import com.wxy.xr.bean.User;
import com.wxy.xr.dao.UserDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.ArrayList;
import java.util.List;


public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {


    @Override
    public boolean save(User bean) {
        Integer id=bean.getId();
        List<Object> args = new ArrayList<>();
        args.add(bean.getPassword());
        args.add(bean.getName());
        args.add(bean.getPhone());
        args.add(bean.getAddress());
        args.add(bean.getBirthday());
        args.add(bean.getEmail());
        args.add(bean.getInterests());
        args.add(bean.getIntro());
        args.add(bean.getJob());
        args.add(bean.getPhoto());
        args.add(bean.getTrait());
        String sql;
        if(id==null || id<1){
            sql="INSERT INTO user(password,name,phone,address,birthday,email,interests,intro,job,photo,trait) values(?,?,?,?,?,?,?,?,?,?,?)";
        } else {
            sql="UPDATE user set password=?, name =?, phone=?, address=?, birthday=?, email=?, interests=?, intro=?, job=?, photo=?, trait=? where id =?";
            args.add(id);
        }
        return tpl.update(sql,args.toArray())>0;
    }

    @Override
    public User get(Integer id) {
        String sql="select id, created_time, password,name,phone,address,birthday,email,interests,intro,job,photo,trait from user where id=?";
        return tpl.queryForObject(sql,new BeanPropertyRowMapper<>(User.class),id);
    }

    @Override
    public List<User> list() {
        String sql="select id, created_time, password,name,phone,address,birthday,email,interests,intro,job,photo,trait from user" ;
        return tpl.query(sql,new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public User get(User user) {
        String sql="select id, created_time, password,name,phone,address,birthday,email,interests,intro,job,photo,trait from user where email=? and password=?";
        List<User> users= tpl.query(sql,new BeanPropertyRowMapper<>(User.class),user.getEmail(),user.getPassword());
        return users.isEmpty()?null:users.get(0);
    }
}
